export interface Teacher {
  id: number;
  name: string;
  gender: string;
  age: number;
  title: string;
  department: string; // 就业学院
  phone: string;
  joinDate: string;
  courses: string;
  successRate: number; // 成才率
  managedClasses: number[]; // 所带班级ID数组
  idNumber?: string; // 身份证号
  teachingProgress?: string; // 教学进度
  status?: 'employed' | 'resigned'; // 状态
}
// 模拟教师数据
export const mockTeachers: Teacher[] = [
  {
    id: 1,
    name: '李教授',
    gender: '男',
    idNumber: '110101197801011234',
    managedClasses:[1,2],

    phone: '13800138001',
    teachingProgress: '专业2',
    joinDate: '2005-09-01',
    department: '人工智能',
    status: 'active'
  },
  {
    id: 2,
    name: '王副教授',
    gender: '女',
    idNumber: '110101198301012345',
    managedClasses: [3],
    phone: '13800138002',
    teachingProgress: '专业1',
    joinDate: '2008-09-01',
    department: '人工智能',
    status: 'active'
  },
  {
    id: 3,
    name: '张讲师',
    gender: '男',
    idNumber: '110101198801013456',
    managedClasses: [4],
    phone: '13800138003',
    teachingProgress: '专业3',
    joinDate: '2012-09-01',
    department: '新媒体',
    status: 'active'
  },
  {
    id: 4,
    name: '刘教授',
    gender: '男',
    idNumber: '110101197301014567',
    managedClasses: [1],
    phone: '13800138004',
    teachingProgress: '专业1',
    joinDate: '1998-09-01',
    department: '数据分析',
    status: 'active'
  },
  {
    id: 5,
    name: '陈助教',
    gender: '女',
    idNumber: '110101199501015678',
    managedClasses: [4],
    phone: '13800138005',
    teachingProgress: '专业2',
    joinDate: '2018-09-01',
    department: '人工智能',
    status: 'active'
  },
  {
    id: 6,
    name: '赵讲师',
    gender: '男',
    idNumber: '110101199101016789',
    managedClasses: [3],
    phone: '13800138006',
    teachingProgress: '专业3',
    joinDate: '2015-09-01',
    department: '新媒体',
    status: 'inactive'
  }
];

// 存储键名
const STORAGE_KEY = 'teachers_data';

// 模拟API函数

// 获取教师列表
export const getTeachers = async (): Promise<Teacher[]> => {
  // 模拟网络延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  
  // 从localStorage读取数据
  const storedData = localStorage.getItem(STORAGE_KEY);
  if (storedData) {
    try {
      return JSON.parse(storedData);
    } catch (error) {
      console.error('解析存储的教师数据失败:', error);
    }
  }
  
  // 如果没有存储的数据或解析失败，返回默认的mock数据
  // 并将mock数据保存到localStorage
  localStorage.setItem(STORAGE_KEY, JSON.stringify(mockTeachers));
  return [...mockTeachers];
};

// 添加教师
export const addTeacher = async (teacherData: Omit<Teacher, 'id'>): Promise<Teacher> => {
  // 模拟网络延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  
  // 获取当前存储的教师列表
  let currentTeachers = [];
  const storedData = localStorage.getItem(STORAGE_KEY);
  if (storedData) {
    try {
      currentTeachers = JSON.parse(storedData);
    } catch (error) {
      console.error('解析存储的教师数据失败:', error);
      currentTeachers = [...mockTeachers];
    }
  } else {
    currentTeachers = [...mockTeachers];
  }
  
  const newTeacher: Teacher = {
    id: Math.max(...currentTeachers.map(t => t.id), 0) + 1,
    ...teacherData
  };
  
  // 添加新教师并保存到localStorage
  currentTeachers.push(newTeacher);
  localStorage.setItem(STORAGE_KEY, JSON.stringify(currentTeachers));
  
  // 更新内存中的mock数据（保持兼容性）
  const existingIndex = mockTeachers.findIndex(t => t.id === newTeacher.id);
  if (existingIndex === -1) {
    mockTeachers.push(newTeacher);
  }
  
  return newTeacher;
};

// 更新教师
export const updateTeacher = async (id: number, teacherData: Partial<Teacher>): Promise<Teacher> => {
  // 模拟网络延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  
  // 获取当前存储的教师列表
  let currentTeachers = [];
  const storedData = localStorage.getItem(STORAGE_KEY);
  if (storedData) {
    try {
      currentTeachers = JSON.parse(storedData);
    } catch (error) {
      console.error('解析存储的教师数据失败:', error);
      currentTeachers = [...mockTeachers];
    }
  } else {
    currentTeachers = [...mockTeachers];
  }
  
  const index = currentTeachers.findIndex(t => t.id === id);
  if (index === -1) {
    throw new Error('教师不存在');
  }
  
  currentTeachers[index] = {
    ...currentTeachers[index],
    ...teacherData
  };
  
  // 保存更新后的数据到localStorage
  localStorage.setItem(STORAGE_KEY, JSON.stringify(currentTeachers));
  
  // 更新内存中的mock数据
  const mockIndex = mockTeachers.findIndex(t => t.id === id);
  if (mockIndex !== -1) {
    mockTeachers[mockIndex] = {
      ...mockTeachers[mockIndex],
      ...teacherData
    };
  }
  
  return currentTeachers[index];
};

// 删除教师
export const deleteTeacher = async (id: number): Promise<boolean> => {
  // 模拟网络延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  
  // 获取当前存储的教师列表
  let currentTeachers = [];
  const storedData = localStorage.getItem(STORAGE_KEY);
  if (storedData) {
    try {
      currentTeachers = JSON.parse(storedData);
    } catch (error) {
      console.error('解析存储的教师数据失败:', error);
      currentTeachers = [...mockTeachers];
    }
  } else {
    currentTeachers = [...mockTeachers];
  }
  
  const index = currentTeachers.findIndex(t => t.id === id);
  if (index === -1) {
    throw new Error('教师不存在');
  }
  
  currentTeachers.splice(index, 1);
  
  // 保存更新后的数据到localStorage
  localStorage.setItem(STORAGE_KEY, JSON.stringify(currentTeachers));
  
  // 更新内存中的mock数据
  const mockIndex = mockTeachers.findIndex(t => t.id === id);
  if (mockIndex !== -1) {
    mockTeachers.splice(mockIndex, 1);
  }
  
  return true;
};
